﻿#include "FMessageBox.h"
#include "ui_FMessageBox.h"

#include <QMetaEnum>
#include <QApplication>
#include <QStyle>


FMessageBox::FMessageBox(QWidget *parent, const QString &title, const QString &text, const QIcon &icon,
                         StandardButton button1, StandardButton button2, StandardButton button3)
    : FDialog(parent)
    , ui(new Ui::FMessageBox)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);

    ui->btn1->setText( staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("StandardButton")).valueToKey(button1) );
    ui->btn2->setText( staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("StandardButton")).valueToKey(button2) );
    ui->btn3->setText( staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("StandardButton")).valueToKey(button3) );

    if (button1 == NoButton) ui->btn1->hide();
    if (button2 == NoButton) ui->btn2->hide();
    if (button3 == NoButton) ui->btn3->hide();

    setWindowTitle(title);
    setText(text);
    setIcon(icon);

    m_btns.push_back(button1);
    m_btns.push_back(button2);
    m_btns.push_back(button3);
}

FMessageBox::~FMessageBox()
{
    delete ui;
}

void FMessageBox::setText(const QString &text)
{
    ui->textLabel->setText(text);
    adjustSize();
}

void FMessageBox::setIcon(const QIcon &icon)
{
    ui->iconLabel->setPixmap(icon.pixmap(ui->iconLabel->size()));
}

FMessageBox::StandardButton FMessageBox::standardButton() const
{
    return m_sb;
}

FMessageBox::StandardButton FMessageBox::information(QWidget *parent, const QString &title, const QString &text)
{
    QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation);
    FMessageBox* box = new FMessageBox(parent,title, text, icon, Ok);
    box->exec();
    return box->standardButton();
}

FMessageBox::StandardButton FMessageBox::question(QWidget *parent, const QString &title, const QString &text)
{
    QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxQuestion);
    FMessageBox* box = new FMessageBox(parent,title, text, icon, Yes, No);
    box->exec();
    return box->standardButton();
}

FMessageBox::StandardButton FMessageBox::warning(QWidget *parent, const QString &title, const QString &text)
{
    QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
    FMessageBox* box = new FMessageBox(parent,title, text, icon, Ok);
    box->exec();
    return box->standardButton();
}

FMessageBox::StandardButton FMessageBox::critical(QWidget *parent, const QString &title, const QString &text)
{
    QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
    FMessageBox* box = new FMessageBox(parent,title, text, icon, Ok);
    box->exec();
    return box->standardButton();
}

void FMessageBox::on_btn1_clicked()
{
    m_sb = m_btns[0];
    accept();
}


void FMessageBox::on_btn2_clicked()
{
    m_sb = m_btns[1];
    accept();
}


void FMessageBox::on_btn3_clicked()
{
    m_sb = m_btns[2];
    accept();
}

